home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1999 June
/
Macworld (1999-06).dmg
/
Shareware World
/
Info
/
For Developers
/
MacZoop2.0.sea
/
MacZoop2.0
/
Required Classes
/
ZClipboard.cpp
< prev
next >
Wrap
Text File
|
1998-07-06
|
10KB
|
391 lines
/*************************************************************************************************
*
*
* MacZoop - "the framework for the rest of us"
*
*
*
* ZClipboard.cpp -- the clipboard object
*
*
*
*
*
* © 1997, Graham Cox
*
*
*
*
*************************************************************************************************/
#include "MacZoop.h"
ZClipboard* gClipboard = NULL;
/*----------------------------------*** PUTDATA ***-----------------------------------*/
/*
put the handle on the clipboard with the type supplied
----------------------------------------------------------------------------------------*/
void ZClipboard::PutData( OSType dataType, Handle someData )
{
FailNILParam( someData );
long len = GetHandleSize( someData );
char hs = HGetState( someData );
HLock( someData );
PutData( dataType, *someData, len );
HSetState( someData, hs );
}
/*----------------------------------*** PUTDATA ***-----------------------------------*/
/*
put arbitrary data on the clipboard with the type supplied
----------------------------------------------------------------------------------------*/
void ZClipboard::PutData( OSType dataType, Ptr dataPtr, const long dataLen )
{
Clear();
AppendData( dataType, dataPtr, dataLen );
SendMessage( clipContentsChanged, (void*) dataType );
}
/*----------------------------------*** PUTDATA ***-----------------------------------*/
/*
put the picture on the clipboard with the type 'PICT'
----------------------------------------------------------------------------------------*/
void ZClipboard::PutData( PicHandle aPicture )
{
PutData( 'PICT', (Handle) aPicture );
}
/*----------------------------------*** PUTTEXT ***-----------------------------------*/
/*
put the text on the clipboard with the type 'TEXT'
----------------------------------------------------------------------------------------*/
void ZClipboard::PutText( Handle textH )
{
PutData( 'TEXT', textH );
}
/*----------------------------------*** PUTTEXT ***-----------------------------------*/
/*
put the text in an arbitrary buffer on the clipboard with the type 'TEXT'
----------------------------------------------------------------------------------------*/
void ZClipboard::PutText( Ptr charBuf, const long textLen )
{
Clear();
AppendText( charBuf, textLen );
}
/*----------------------------------*** APPENDDATA ***--------------------------------*/
/*
add the data to the clipboard with the type supplied
----------------------------------------------------------------------------------------*/
void ZClipboard::AppendData( OSType dataType, Handle someData )
{
FailNILParam( someData );
long len = GetHandleSize( someData );
char hs = HGetState( someData );
HLock( someData );
AppendData( dataType, *someData, len );
HSetState( someData, hs );
SendMessage( clipContentsAppended, (void*) dataType );
}
/*----------------------------------*** APPENDDATA ***--------------------------------*/
/*
add the arbitrary data to the clipboard with the type supplied
----------------------------------------------------------------------------------------*/
void ZClipboard::AppendData( OSType dataType, Ptr dataPtr, const long dataLen )
{
FailOSErr( PutScrap( dataLen, dataType, dataPtr ));
}
/*----------------------------------*** APPENDDATA ***--------------------------------*/
/*
add the picture to the clipboard with the type 'PICT'
----------------------------------------------------------------------------------------*/
void ZClipboard::AppendData( PicHandle aPicture )
{
AppendData( 'PICT', (Handle) aPicture );
}
/*----------------------------------*** APPENDTEXT ***--------------------------------*/
/*
add the text to the clipboard with the type 'TEXT'
----------------------------------------------------------------------------------------*/
void ZClipboard::AppendText( Handle textH )
{
AppendData( 'TEXT', textH );
}
/*----------------------------------*** APPENDTEXT ***--------------------------------*/
/*
add the text in an arbitrary buffer to the clipboard with the type 'TEXT'
----------------------------------------------------------------------------------------*/
void ZClipboard::AppendText( Ptr charBuf, const long textLen )
{
Handle t;
FailOSErr( PtrToHand( charBuf, &t, textLen ));
try
{
AppendText( t );
}
catch( OSErr err )
{
DisposeHandle( t );
throw err;
}
DisposeHandle( t );
}
/*-------------------------------------*** CLEAR ***----------------------------------*/
/*
make the clipboard empty
----------------------------------------------------------------------------------------*/
void ZClipboard::Clear()
{
FailOSErr( ZeroScrap());
SendMessage( clipContentsCleared, NULL );
}
/*------------------------------------*** GETDATA ***---------------------------------*/
/*
get the data from the clipboard with the type requested
----------------------------------------------------------------------------------------*/
Handle ZClipboard::GetData( OSType dataType )
{
Handle h = NULL;
long result, offset;
if ( dataType == Wild_Card )
GetWildcardType( &dataType );
if ( QueryType( dataType ))
{
FailNIL( h = NewHandle( 0 ));
result = GetScrap( h, dataType, &offset);
if (result <= 0)
{
DisposeHandle( h );
FailOSErr( noTypeErr );
}
}
return h;
}
/*----------------------------------*** QUERYTYPE ***---------------------------------*/
/*
does the clipboard have this data type?
----------------------------------------------------------------------------------------*/
Boolean ZClipboard::QueryType( OSType dataType )
{
long result, offset;
if ( dataType == Wild_Card )
result = GetWildcardType( &dataType );
else
result = GetScrap( NULL, dataType, &offset );
return ( result > 0 );
}
/*----------------------------------*** GETDATASIZE ***-------------------------------*/
/*
how big is the data of this type?
----------------------------------------------------------------------------------------*/
long ZClipboard::GetDataSize( OSType dataType )
{
long result, offset;
if ( dataType == Wild_Card )
GetWildcardType( &dataType );
result = GetScrap( NULL, dataType, &offset );
if ( result > 0 )
return result;
else
return -1;
}
/*--------------------------------*** GETCLIPSTATUS ***-------------------------------*/
/*
what state is the clipboard in?
----------------------------------------------------------------------------------------*/
short ZClipboard::GetClipStatus()
{
PScrapStuff ps;
ps = InfoScrap();
return ps->scrapCount;
}
/*-------------------------------*** GETWILDCARDTYPE ***------------------------------*/
/*
what is the type of the first item of data on the clipboard? Returns TRUE if there is
one, else FALSE.
----------------------------------------------------------------------------------------*/
Boolean ZClipboard::GetWildcardType( OSType* aType )
{
PScrapStuff ps;
ps = InfoScrap();
if ( ps->scrapState > 0 &&
ps->scrapSize > 0 &&
ps->scrapHandle != NULL )
{
*aType = **(OSType**) ps->scrapHandle;
return TRUE;
}
return FALSE;
}
/*----------------------------------*** COUNTTYPES ***--------------------------------*/
/*
how many different types of data are there on the clipboard?
----------------------------------------------------------------------------------------*/
short ZClipboard::CountTypes()
{
PScrapStuff ps;
long sLen, sOffset, dLen;
short n = 0;
char hs;
ps = InfoScrap();
// n.b. if scrap has been offloaded to disc, this doesn't work and returns 0.
if ( ps->scrapState > 0 &&
ps->scrapSize > 0 &&
ps->scrapHandle != NULL )
{
// need to work through the handle determining the number of data entries
// that exist there. We'll temporarily lock it down and use a running pointer.
hs = HGetState( ps->scrapHandle );
HLock( ps->scrapHandle );
Ptr p = *ps->scrapHandle;
sLen = GetHandleSize( ps->scrapHandle );
sOffset = 0;
while( sOffset < sLen )
{
n++; // one more type
p += sizeof( OSType ); // skip type bytes
dLen = *(long*) p; // read data length
// if dLen is odd, round up to even number of bytes
if ( dLen & 1 )
dLen++;
p += sizeof( long ) + dLen; // point to next item
sOffset += sizeof( OSType ) + sizeof( long ) + dLen;
}
HSetState( ps->scrapHandle, hs );
}
return n;
}
/*----------------------------------*** GETINDTYPE ***--------------------------------*/
/*
return the type of clipboard data indexed from 1 to the value returned by CountTypes.
----------------------------------------------------------------------------------------*/
OSType ZClipboard::GetIndType( const short index )
{
OSType theType = Wild_Card;
PScrapStuff ps;
long dLen, sLen, sOffset;
short k = 0;
char hs;
ps = InfoScrap();
if ( ps->scrapState > 0 &&
ps->scrapSize > 0 &&
ps->scrapHandle != NULL )
{
hs = HGetState( ps->scrapHandle );
HLock( ps->scrapHandle );
Ptr p = *ps->scrapHandle;
sLen = GetHandleSize( ps->scrapHandle );
sOffset = 0;
while( sOffset < sLen )
{
k++;
if ( k == index )
{
theType = *(OSType*) p;
break;
}
p += sizeof( OSType );
dLen = *(long*) p;
if ( dLen & 1 )
dLen++;
sOffset += sizeof( OSType ) + sizeof( long ) + dLen;
p += sizeof( long ) + dLen;
}
HSetState( ps->scrapHandle, hs );
}
return theType;
}